<?php
$objUserForm = new BM_PHPForm();
$objUserForm->UseLocator();
$objUserForm->SetIndent("          ");
$arrRoles = RoleController::findAll();

if (isset($_REQUEST['msg']) && $_REQUEST['msg'] == "err"){
	$strUser = $_REQUEST['user'];
	$objUserForm->SetError("delete","User [$strUser] Cann't Be Deleted .");
}
if (isset($_REQUEST['msg']) && $_REQUEST['msg'] == "inf"){
	$strUser = $_REQUEST['user'];
	$objUserForm->SetInfor("delete","User [$strUser] Deleted Successfully!");
}

if ($objUserForm->IsPost()){
	$entUser0 = UserController::find($objUserForm->arrPostData['username']);
	$objUserForm->ChEmptiness("username", "Username is empty.");
	$objUserForm->ChWithRegex("username", "Username Have Invalid Chars.", "/^([a-zA-Z0-9_\\.\\-])*$/");
	$objUserForm->ChMinLength("username", "Username must not be less than 4 chars", 4);
	$objUserForm->ChMaxLength("username", "Username must not be more than 40 chars.", 40);
	$objUserForm->ChCustomVal("username", "Username Exist ! Please Choose Another ." , $entUser0->username == "guest");
	$objUserForm->ChEmptiness("name"	, "Name is empty.");
	$objUserForm->ChMaxLength("name"	, "Name must not be more than 40 chars.", 40);
	$objUserForm->ChWithRegex("name"	, "Name Have Invalid Chars.", "/^([a-zA-Z0-9_\\.\\-\s])*$/");
	$objUserForm->ChEmptiness("password", "Password is empty.");
	$objUserForm->ChMinLength("password", "Password must not be less than 4 chars", 4);
	$objUserForm->ChMaxLength("password", "Password must not be more than 12 chars.", 12);
	$objUserForm->ChCustomVal("password", "Passwords must be same.",$objUserForm->arrPostData['password']==$objUserForm->arrPostData['password2'] );
	
	if ($objUserForm->IsValid()){
		$entUser = new User($objUserForm->arrPostData['username'],md5($objUserForm->arrPostData['password']),$objUserForm->arrPostData['name']);
		UserController::save($entUser);
		$uroles = array();
		foreach($arrRoles as $role){
			if(isset($objUserForm->arrPostData[$role->id])){
				$uroles[]=$role->id;
			}
		}
		if (!sizeof($uroles) == 0){
			UserController::saveRoles($entUser,$uroles);
		}
		$objUserForm->SetInfor("submit","User Added Successfully !");
		header("Location: ../$www/index_user.php?action=update&id=$entUser->username&msg=add");
	}	
}

print $objUserForm->ErrMsgs();
print $objUserForm->InfMsgs();

print $objUserForm->Open("userform","User Registration Form");
print $objUserForm->HiddenField("action","$action");
print $objUserForm->TextBox("username" 	,"Username:","");
print $objUserForm->TextBox("name" 		,"Name_:","");
print $objUserForm->TextBox("password" 	,"Password:","",array(),"password");
print $objUserForm->TextBox("password2" ,"Confirm Password:","",array(),"password");
print "		<div style=\"padding-left: 100px;width: 30px;\">Roles:</div>\r\n";
foreach ($arrRoles as $role){
	print $objUserForm->CheckBox("$role->id",$role->name,array(),false,$role->id);
}
print $objUserForm->SubmitButton("submit", "Submit", array(), "submit");
//print $objUserForm->Reset("reset", "Reset", array(),"reset");

print $objUserForm->Close();
?>